package wiiusej.apptest.activityTasks;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import wiiusej.apptest.statistics.AccelerationPaquet;
import wiiusej.apptest.statistics.performance.ClassTrackName;
import wiiusej.apptest.statistics.performance.Track;

public class HeuristicStandAloneImpl extends HeuristicAlgorithm{

	//logger
	private Logger resultsLog;
		
	//COM QUEUES
	
	private BlockingQueue<HashMap<GameModes, Integer>> toListenerResults;
	
	private HashMap<GameModes, Integer> results = new HashMap<GameModes, Integer>();
	
	
	public HeuristicStandAloneImpl(BlockingQueue comQueue, 
			BlockingQueue<HashMap<GameModes, Integer>> toListenerResults) {		
		
		super(comQueue);		
		
		PropertyConfigurator.configure("log4j.properties");
		resultsLog = Logger.getLogger("realTimeAccuracy");
		
		resultsLog.info("INIT HEURISTIC ALGORITHM!!");
		this.toListenerResults = toListenerResults;
		
		initResults();
	}
	
	@Override
	protected void recognizeMotion(int meanX, int meanY, int meanZ, 
		SimpleDateFormat tsFormat, AccelerationPaquet nextToLastChecked){
		
		resultsLog.info("Receiving a packet");
		
		//motion recognition
		GameModes motion = giveFeedback(meanZ, meanZ, meanZ, tsFormat, nextToLastChecked);
		
		resultsLog.info(motion.ordinal() + "Index: " + results.get(motion)+1);
		
		//upload counter
		
		results.put(motion, results.get(motion)+1);
		
		resultsLog.info("Printing Results...");
		//printResults();
		sendResults();
		
		resultsLog.info("Results sent!!!");
	}

	private void sendResults(){
		SimpleDateFormat tsFormat = new SimpleDateFormat("mm:ss:SS");
		resultsLog.info("Sending results..." + tsFormat.format(System.currentTimeMillis()));
		try {
			toListenerResults.put(results);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}		
	}
	
	private void initResults(){
		results.put(GameModes.SLOW_MOTION, 0);
		results.put(GameModes.BIG_SOFT, 0);
		results.put(GameModes.ROBOTIC, 0);
		results.put(GameModes.NO_MODE, 0);
		//results.put(GameModes.REST, 0);		
	}
	
	
	private void printResults(){
	
		resultsLog.info("");
		resultsLog.info("RESULTS:");
		
		Iterator it = results.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry e = (Map.Entry)it.next();
			resultsLog.info(e.getKey() + " " + e.getValue());
		}
		
		resultsLog.info("");
		
	}
	
	
	
		
}
